/**
 * Create on 2010-4-22 04:13:06
 * @author JIXF
 * @E-Mail:jixf@mochasoft.com.cn
 */

package com.lq.util;

import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

/**
 * <a href="DateUtil.java.html"><b><i>View Source</i></b></a>
 * 
 * @author JIXF
 * 
 */

public class DBConnection {

	private static Properties pro;

	static {
		try {
			pro = PropertiesUtil.load("db.properties");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static Connection getConnectionJNDI() {
		Connection con = null;
		try {
			Context ic = new InitialContext();
			DataSource ds = (DataSource) ic.lookup(pro
					.getProperty("jndi.source"));
			con = ds.getConnection();
		} catch (Exception e) {
			throw new RuntimeException("getConnectionJNDI failed!");
		}
		return con;
	}

	public static Connection getConnectionOracleJDBC() {
		Connection con = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
			String url = "jdbc:oracle:thin:@" + pro.getProperty("oracle.url");
			String user = pro.getProperty("oracle.user");
			String password = pro.getProperty("oracle.password");
			con = DriverManager.getConnection(url, user, password);
		} catch (Exception e) {
			throw new RuntimeException("getConnectionJDBC failed!");
		}
		return con;
	}

	public static Connection getConnectionMySQLJDBC() {
		Connection con = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://" + pro.getProperty("mysql.url") + "/"
					+ pro.getProperty("mysql.db") + "?user="
					+ pro.getProperty("mysql.user") + "&password="
					+ pro.getProperty("mysql.password")
					+ "&useUnicode=true&amp;characterEncoding=GBK";
			con = DriverManager.getConnection(url);
		} catch (Exception e) {
			throw new RuntimeException("getConnectionMySQLJDBC failed!");
		}
		return con;
	}

	public static void begin(Connection con) {
		try {
			con.setAutoCommit(false);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void rollback(Connection con) {
		try {
			con.rollback();
			con.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void commit(Connection con) {
		try {
			con.commit();
			con.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void close(ResultSet rs, Statement st, Connection con) {
		try {
			if (rs != null) {
				rs.close();
				rs = null;
			}
			if (st != null) {
				st.close();
				st = null;
			}
			if (con != null) {
				con.close();
				con = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void close(ResultSet rs, PreparedStatement st,
			CallableStatement cs, Connection con) {
		try {

			if (rs != null) {
				rs.close();
				rs = null;
			}
			if (st != null) {
				st.close();
				st = null;
			}
			if (cs != null) {
				cs.close();
				cs = null;
			}
			if (con != null) {
				con.close();
				con = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void close(ResultSet rs, PreparedStatement st, Connection con) {
		try {
			if (rs != null) {
				rs.close();
				rs = null;
			}
			if (st != null) {
				st.close();
				st = null;
			}
			if (con != null) {
				con.close();
				con = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
